package com.example.echoewish.ui.my_activity.login_and_register_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.User;
import com.example.echoewish.ui.EchoWishCompatActivity;

public class RegisterActivity extends EchoWishCompatActivity {

    private EditText registerAccountEditText;
    private EditText registerPasswordEditText;
    private EditText registerPasswordComfirEditText;
    private Button registerButton;

    private RegisterViewModel registerViewModel;

    public Handler registerHandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.d("test_activity", "handleMessage: ");
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(RegisterActivity.this,"注册失败，请检查网络",Toast.LENGTH_SHORT).show();
                    break;
                case Constant.REGISTER_FAIL_MESSAGE:
                    Toast.makeText(RegisterActivity.this,"注册失败，用户名重复",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_register);
    }

    public void initView()
    {
        registerAccountEditText=findViewById(R.id.register_account_edit_text);
        registerPasswordComfirEditText=findViewById(R.id.register_comfir_password_edit_text);
        registerPasswordEditText=findViewById(R.id.register_password_edit_text);
        registerButton=findViewById(R.id.register_button);


        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    public void initViewModel()
    {
        registerViewModel= new ViewModelProvider(RegisterActivity.this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(RegisterViewModel.class);
    }

    public void observeLiveData()
    {
        registerViewModel.newUserMutableLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                registerViewModel.registerNewUser();
            }
        });

        registerViewModel.responseMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Message message=new Message();
                if(s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                {
                    message.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    registerHandler.sendMessage(message);
                }
                else if(s.equals(Constant.REGISTER_FAIL))
                {
                    message.what= Constant.REGISTER_FAIL_MESSAGE;
                    registerHandler.sendMessage(message);
                }
                else if(s.equals(Constant.HTTPS_GET_CALLBACK_FAIL))
                {
                    message.what= Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE;
                    registerHandler.sendMessage(message);
                }
            }
        });
    }

    private void register()
    {
        if(registerAccountEditText.getText().toString().equals("")||registerPasswordEditText.getText().toString().equals(""))
        {
            Toast.makeText(RegisterActivity.this,"账户和密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(registerPasswordComfirEditText.getText().toString().equals(""))
        {
            Toast.makeText(RegisterActivity.this,"请确认密码",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!registerPasswordComfirEditText.getText().toString().equals(registerPasswordEditText.getText().toString()))
        {
            Toast.makeText(RegisterActivity.this,"两次输入的密码不同 请重新输入",Toast.LENGTH_SHORT).show();
            return;
        }

        User newRegisterUser=new User();
        newRegisterUser.account=registerAccountEditText.getText().toString();
        newRegisterUser.password=registerPasswordEditText.getText().toString();

        registerViewModel.newUserMutableLiveData.setValue(newRegisterUser);

        registerAccountEditText.setText("");
        registerPasswordEditText.setText("");
        registerPasswordComfirEditText.setText("");
    }
}